namespace SmartSoft.SystemForms
{
    using SmartSoft;
    using SmartSoft.Controls;
    using SmartSoft.ADP;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormCompanyList : FormControl
    {
        private LViewControl CompanyList;
        private IContainer components;
        private ColumnHeader Header1;
        private ColumnHeader Header2;
        private bool ib_NoEscaple;

        public FormCompanyList()
        {
            this.InitializeComponent();
            this.GenerateItems();
        }

        public FormCompanyList(bool pb_NoEscape)
        {
            this.InitializeComponent();
            this.GenerateItems();
            this.ib_NoEscaple = pb_NoEscape;
        }

        private void CompanyList_ItemActivate(object sender, EventArgs e)
        {
            AppInstance.CompanyRegister(this.CompanyList.Items[this.CompanyList.SelectedIndices[0]].SubItems[2].Text);
            base.Close();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void GenerateItems()
        {
            //DataTable companies = new CompanyDataAccess().GetCompanies();
            DataTable companies = new CompanyDataAccess().GetCompaniesByUser(AppInstance.UserName);
            this.CompanyList.Items.Clear();
            int num = 0;
            foreach (DataRow row in companies.Rows)
            {
                if (((string) row["Ma_Dvcs"]) == AppInstance.Subsidiary)
                {
                    this.CompanyList.Items.Add((string) row["Ten_Dvcs"]);
                    DateTime time = (DateTime) row["Ngay_Bd_Ht"];
                    this.CompanyList.Items[num].SubItems.Add(time.ToShortDateString());
                    this.CompanyList.Items[num].SubItems.Add((string) row["Ma_Dvcs"]);
                    this.CompanyList.Items[num].Group = this.CompanyList.Groups[0];
                    num++;
                }
            }

            foreach (DataRow row2 in companies.Rows)
            {
                if (((string) row2["Ma_Dvcs"]) != AppInstance.Subsidiary)
                {
                    this.CompanyList.Items.Add((string) row2["Ten_Dvcs"]);
                    DateTime time2 = (DateTime) row2["Ngay_Bd_Ht"];
                    this.CompanyList.Items[num].SubItems.Add(time2.ToShortDateString());
                    this.CompanyList.Items[num].SubItems.Add((string) row2["Ma_Dvcs"]);
                    this.CompanyList.Items[num].Group = this.CompanyList.Groups[1];
                    num++;
                }
            }
            this.CompanyList.Items[0].Selected = true;
        }

        private void InitializeComponent()
        {
            ListViewGroup group = new ListViewGroup("Đang l\x00e0m việc với", HorizontalAlignment.Left);
            ListViewGroup group2 = new ListViewGroup("C\x00e1c đơn vị c\x00f2n lại", HorizontalAlignment.Left);
            ListViewItem item = new ListViewItem(new string[] { "Công ty Cổ phần SSSoft", "01/01/2009" }, -1);
            ListViewItem item2 = new ListViewItem(new string[] { "Công ty Kiểm toán Ho\x00e0ng Cầu", "01/01/2009" }, -1);
            ListViewItem item3 = new ListViewItem(new string[] { "Công ty thương mại Ho\x00e0ng Gia", "01/01/2009" }, -1);
            this.CompanyList = new LViewControl();
            this.Header1 = new ColumnHeader();
            this.Header2 = new ColumnHeader();
            base.SuspendLayout();
            this.CompanyList.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.CompanyList.Columns.AddRange(new ColumnHeader[] { this.Header1, this.Header2 });
            this.CompanyList.Font = new Font("Arial", 8.25f);
            this.CompanyList.FullRowSelect = true;
            group.Header = "Đang l\x00e0m việc với";
            group.Name = "GroupItem01";
            group2.Header = "C\x00e1c đơn vị c\x00f2n lại";
            group2.Name = "GroupItem02";
            this.CompanyList.Groups.AddRange(new ListViewGroup[] { group, group2 });
            this.CompanyList.HeaderStyle = ColumnHeaderStyle.None;
            item.Group = group;
            item.StateImageIndex = 0;
            item2.Group = group2;
            item2.StateImageIndex = 0;
            item3.Group = group2;
            item3.StateImageIndex = 0;
            this.CompanyList.Items.AddRange(new ListViewItem[] { item, item2, item3 });
            this.CompanyList.Location = new Point(12, 12);
            this.CompanyList.MultiSelect = false;
            this.CompanyList.Name = "CompanyList";
            this.CompanyList.Size = new Size(0x1ab, 0xd1);
            this.CompanyList.TabIndex = 0;
            this.CompanyList.UseCompatibleStateImageBehavior = false;
            this.CompanyList.View = View.Details;
            this.CompanyList.ItemActivate += new EventHandler(this.CompanyList_ItemActivate);
            this.Header1.Text = "Tên đơn vị";
            this.Header1.Width = 0x147;
            this.Header2.Text = "Ngày bắt đầu";
            this.Header2.TextAlign = HorizontalAlignment.Center;
            this.Header2.Width = 0x4f;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.ClientSize = new Size(0x1c3, 0xe9);
            base.Controls.Add(this.CompanyList);
            //base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.KeyPreview = true;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FormCompanyList";
            base.ShowInTaskbar = false;
            this.Text = "Danh mục đơn vị cơ sở: F3-Sửa";
            base.ResumeLayout(false);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F3)
            {
                FormCompanyEditor editor = new FormCompanyEditor();
                editor.CompanyCode = this.CompanyList.Items[this.CompanyList.SelectedIndices[0]].SubItems[2].Text;
                editor.ShowDialog();
                if (editor.CheckAccepted)
                {
                    this.CompanyList.Items[this.CompanyList.SelectedIndices[0]].SubItems[0].Text = editor.CompanyFullName;
                    this.CompanyList.Items[this.CompanyList.SelectedIndices[0]].SubItems[1].Text = editor.StartDate;
                }
                return true;
            }
            if (keyData == Keys.Return)
            {
                AppInstance.CompanyRegister(this.CompanyList.Items[this.CompanyList.SelectedIndices[0]].SubItems[2].Text);
                base.Close();
                return true;
            }
            if ((keyData == Keys.Escape) && !this.ib_NoEscaple)
            {
                base.Close();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}

